window.APIURL = "/api/";

/**
 * 接管所有的 ajax 请求
 * @param setting
 */
function myAjax(setting) {
    setting.url = APIURL + setting.url;
    // 接管 success 和 error 事件
    let successFunction;
    if (setting.hasOwnProperty("success")) {
        successFunction = setting.success;
        delete setting.success;
    } else {
        successFunction = function (result) {};
    }
    let errorFunction;
    if (setting.hasOwnProperty("error")) {
        errorFunction = setting.error;
        delete setting.error;
    } else {
        errorFunction = function (result) {};
    }
    bui.ajax(
        setting
    ).done(function(result) {
        if (!result.success) {
            alert(result.data)
        } else {
            successFunction(result);
        }
    }).fail(function (result) {
        errorFunction(result);
    })
}

/**
 * 扩展 Date 方法
 * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S")
 * @param fmt
 * @returns {*}
 * @constructor
 * @author IT_Debug
 */
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o){
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};

/**
 * 获取 # 后面具体参数
 * @returns {string}
 * @constructor
 */
function getUrlPara() {
    var url = document.location.toString();
    var arrUrl = url.split("#");
    if (arrUrl.length >= 2 ) {
        return arrUrl[1];
    } else {
        return "";
    }

}
